package com.xxxx.generator.vue;

import com.xxxx.generator.util.DbUtil;
import com.xxxx.generator.util.Field;
import com.xxxx.generator.util.FreemarkerUtil;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VueGenerator {

    //  模块名称
    static String MODULE = "business"; // business, file, system
    //  vue生成路径
    static String toVuePath = "admin\\src\\views\\admin\\";
    //  mybatis-generator所在路径
    static String generatorConfigPath = "server\\src\\main\\resources\\generator\\generator-config.xml";

    /**
     * 得先执行EnumGenerator,因为vue模板里用了枚举类
     */
    public static void main(String[] args) throws Exception {
        String module = MODULE;

        /**
         * 整合mybatis-generator
         * 从其配置文件中读取相关参数
         * 需要加入dom4j依赖
         */
        // 只生成配置文件中的第一个table节点
        File file = new File(generatorConfigPath);
        SAXReader reader=new SAXReader();
        //读取xml文件到Document中
        Document doc=reader.read(file);
        //获取xml文件的根节点
        Element rootElement=doc.getRootElement();
        //读取context节点
        Element contextElement = rootElement.element("context");
        //定义一个Element用于遍历
        Element tableElement;
        //取第一个“table”的节点
        tableElement=contextElement.elementIterator("table").next();
        String Domain = tableElement.attributeValue("domainObjectName");
        String tableName = tableElement.attributeValue("tableName");
        String tableNameCn = DbUtil.getTableComment(tableName);
        String domain = Domain.substring(0, 1).toLowerCase() + Domain.substring(1);
        System.out.println("表："+tableElement.attributeValue("tableName"));
        System.out.println("Domain："+tableElement.attributeValue("domainObjectName"));

        List<Field> fieldList = DbUtil.getColumnByTableName(tableName);
        Map<String, Object> map = new HashMap<>();
        map.put("Domain", Domain);
        map.put("domain", domain);
        map.put("tableNameCn", tableNameCn);
        map.put("module", module);
        map.put("fieldList", fieldList);

        // 生成vue
        FreemarkerUtil.initConfig("vue.ftl");
        FreemarkerUtil.generator(toVuePath + domain + ".vue", map);
    }
}
